# -*- coding: utf-8 -*-
from common import trace


class Meta(type):
    @classmethod
    @trace
    def __prepare__(cls, name, bases):
        """构建 cls　时调用"""
        cls.static_x = 'static_x'

        return super().__prepare__(name, bases)


class A(metaclass=Meta):
    x = 10

    def __init__(self, name):
        self.name = name


def prepare_test():
    a = A("jack")
    print(A.x)
    print(A.static_x)

    print(isinstance(a, A))
    print(issubclass(A, Meta))
    print(isinstance(A, Meta))


class Meta2(type):

    @trace
    def __instancecheck__(self, instance):
        return super().__instancecheck__(self)

    @trace
    def __subclasscheck__(self, subclass):
        if subclass is int:
            return True
        else:
            return False



class SubMeta2(metaclass=Meta2):
    pass


def instance_test():
    o = SubMeta2()

    print(isinstance(123, SubMeta2))
    print(isinstance(o, SubMeta2))
    print(isinstance(SubMeta2, Meta2))
    print(issubclass(int, SubMeta2))
    print(issubclass(str, SubMeta2))


if __name__ == '__main__':
    # prepare_test()
    instance_test()

